﻿using System.IO;
using MaxStoreShop.Core.Contracts;
using Microsoft.Win32;

namespace MaxStoreShop.Core.Helpers
{
    public class PhotoHelper : IPhotoHelper
    {
        public byte[] BrowseAndLoadPhoto()
        {
            var ofd = new OpenFileDialog
            {
                Filter = "Изображения|*.bmp;*.jpg;*.png",
                Title = "Загрузка изображения"
            };

            return ofd.ShowDialog() != true ? null : LoadPhoto(ofd.FileName);
        }

        static byte[] LoadPhoto(string fileName)
        {
            using (
                var reader =
                    new BinaryReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                var imageBytes = new byte[reader.BaseStream.Length];
                imageBytes = reader.ReadBytes(imageBytes.Length);
                reader.Close();
                return imageBytes;
            }
        }
    }
}
